package com.ionehe.message;

import com.google.common.base.Preconditions;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

public class RabbitMessageConverter implements MessageConverter {
 
    private GenericMessageConverter delegate;
     
//  private final String delaultExprie = String.valueOf(24 * 60 * 60 * 1000);
     
    public RabbitMessageConverter(GenericMessageConverter genericMessageConverter) {
        Preconditions.checkNotNull(genericMessageConverter);
        this.delegate = genericMessageConverter;
    }
     
    @Override
    public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
//      messageProperties.setExpiration(delaultExprie);
        com.ionehe.model.api.Message message = (com.ionehe.model.api.Message)object;
        messageProperties.setDelay(message.getDelayMills());
        return this.delegate.toMessage(object, messageProperties);
    }
 
    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        com.ionehe.model.api.Message msg = (com.ionehe.model.api.Message) this.delegate.fromMessage(message);
        return msg;
    }
 
}